package com.sqz.mybatisplus.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqz.mybatisplus.demo.mapper.MybatisPlusTestMapper;
import com.sqz.mybatisplus.demo.model.User;
import com.sqz.mybatisplus.demo.service.IMybatisPlusTestService;
import com.sqz.mybatisplus.demo.service.impl.MybatisPlusTestService;
import com.sqz.mybatisplus.demo.util.InstanceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.*;

import static com.sqz.mybatisplus.demo.util.HardWareUtils.getMac;


/**
 * @author sqz
 * @Description: mybatis 常用注解 控制层
 * @date 2020/06/25
 */
@RestController
@RequestMapping(MybatisAnnotationController.PATH)
@Slf4j
public class MybatisAnnotationController {

    /***
     * 控制层信息
     */
    private static final String CONTENT = "mybatis层接口:";

    /***
     * 访问路径
     */
    public final static String PATH = "/mybatis";

    @Autowired
    private MybatisPlusTestMapper mybatisPlusTestMapper;
    @Autowired
    private IMybatisPlusTestService mybatisPlusTestService;

    /**
     * mybatis Select 注解
     */
    @GetMapping("/select")
    public String select() {

        // 先插入（批量）
        Collection<User> entityList = new ArrayList<>();
        User user1 = InstanceUtil.getUser(CONTENT + "mybatis");
        User user2 = InstanceUtil.getUser(CONTENT + "mybatis");
        User user3 = InstanceUtil.getUser(CONTENT + "mybatis");
        User user4 = InstanceUtil.getUser(CONTENT + "mybatis");
        User user5 = InstanceUtil.getUser(CONTENT + "mybatis");
        entityList.add(user1);
        entityList.add(user2);
        entityList.add(user3);
        entityList.add(user4);
        entityList.add(user5);
        mybatisPlusTestService.saveBatch(entityList);
        entityList.forEach(s -> log.info("\r\n批量新增：\r\n" + s));


        List<User> userList;

        userList = mybatisPlusTestMapper.selectAll();
        userList.forEach(s ->log.info(s.toString()));

        userList = mybatisPlusTestMapper.selectByPassword(getMac());
        userList.forEach(s ->log.info(s.toString()));

        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(User::getPassword,getMac());
        mybatisPlusTestService.remove(wrapper);

        return "OK";
    }



}
